package com.ph.shopping.facade.member.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.ph.shopping.common.util.container.ParamVerifyUtil;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.duboresult.DubboResult;
import com.ph.shopping.facade.member.dto.BankBindInfoDto;
import com.ph.shopping.facade.member.dto.BankCardInfoPageDto;
import com.ph.shopping.facade.member.entity.BankCardInfo;
import com.ph.shopping.facade.member.service.IBankCardBindService;
import com.ph.shopping.facade.member.service.dbservice.facade.IBankCardDbService;
import com.ph.shopping.facade.member.vo.UnbundBankCardInfoVo;

/**
 * 
 * @ClassName: BankCardService
 * @Description:银行卡绑定服务类
 * @author tony
 * @date 2017年3月14日
 *
 */
@Component
@Service(version = "1.0.0")
public class BankCardService implements IBankCardBindService {

	private static final Logger loggger = LoggerFactory.getLogger(BankCardService.class);

	@Autowired
	private IBankCardDbService bankCardDbService;

	@Override
	public Result bindBankCardInfo(BankBindInfoDto dto) {
		loggger.info("绑定银行卡参数：BankBindInfoDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { 
				"accountId",
				"cardNum", 
				"ownName", 
				"idCard", 
				"createUser", 
				"userType", 
				"bankName" 
			};
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = bankCardDbService.bindBankCardInfo(dto);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			loggger.error("绑定银行卡信息错误", e);
		}
		return result;
	}

	@Override
	public Result UnbundBankCardInfo(UnbundBankCardInfoVo unbundBankCardInfo) {
		Result result = DubboResult.getDefResult();
		loggger.info("解绑银行卡参数：UnbundBankCardInfoVo = {} ", JSON.toJSONString(unbundBankCardInfo));
		try {
			String[] fields = { "cardNum" ,"userId" ,"userType"};
			if (ParamVerifyUtil.entityIsNotNullByField(unbundBankCardInfo, fields)) {
				result = bankCardDbService.UnbundBankCardInfo(unbundBankCardInfo);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			loggger.error("解绑银行卡错误", e);
		}
		return result;
	}

	@Override
	public Result getBankCardInfoList(BankCardInfo bankCard) {
		loggger.info("获取用户绑定银行卡列表参数：BankCardInfo = {} ", JSON.toJSONString(bankCard));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "accountId" , "userType"};
			if (ParamVerifyUtil.entityIsNotNullByField(bankCard, fields)) {
				result = bankCardDbService.queryBankCardInfoList(bankCard);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			loggger.error("获取银行卡数据列表错误", e);
		}
		return result;
	}

	@Override
	public Result getBankCardInfoByPage(BankCardInfoPageDto dto) {
		loggger.info("根据分页数据获取银行卡列表参数：BankCardInfoPageDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "accountId", "pageSize", "pageNum" , "userType"};
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = bankCardDbService.queryBankCardInfoByPage(dto);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			loggger.error("根据分页信息查询银行卡数据错误", e);
		}
		return result;
	}

	@Override
	public Result getBankCardInfoById(Long id) {
		loggger.info("根据id 获取银行卡信息：id >> " + id);
		Result result = DubboResult.getDefResult();
		try {
			if (id != null) {
				result = bankCardDbService.queryBankCardInfoById(id);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			loggger.error("根据银行卡ID 查询数据错误", e);
		}
		return result;
	}


	@Override
	public Result getBankCodeAndNameByCode(String bankCode) {
		loggger.info("根据bankCode 获取银行卡name信息：bankCode >> " + bankCode);
		Result result = DubboResult.getDefResult();
		try {
			if (StringUtils.isNotBlank(bankCode)) {
				result = bankCardDbService.queryBankCodeAndNameByCode(bankCode);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			loggger.error("根据bankCode 获取银行卡name信息错误", e);
		}
		return result;
	}

	@Override
	public Result getBankCardInfo(Long userId, Byte userType) {
		loggger.info("根据用户信息获取绑定的银行卡信息：userId >> " + userId + " userType >> " + userType);
		Result result = DubboResult.getDefResult();
		try {
			if (userId != null && userType != null) {
				result = bankCardDbService.queryBankCardInfo(userId,userType);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			loggger.error("根据用户信息获取绑定的银行卡信息错误", e);
		}
		return result;
	}

}
